package com.lhkj.ct.admin.modules.gauge.scale.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Validate;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblDiagItem;
import com.lhkj.ct.meta.modules.gauge.service.DiagItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *      医嘱项目 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-11-02
 */
@RestController
@RequestMapping("api/gauge/scale/diag/item")
@Api(tags = {"6.6医嘱项目"})
@Validated
public class DiagItemController {

    @Resource
    private DiagItemService diagItemService;

    @GetMapping(value = "list")
    @ApiOperation(value = "列表接口")
    @ApiOperationSupport(order = 1)
    //@RequiresPermissions(value = {"system:diagItem:list"})
    public ActionResult<IPage<TblDiagItem>> list(TblDiagItem entity, PageQuery pageQuery) {
        return ActionResult.success(diagItemService.selectPages(entity, pageQuery));
    }

    @GetMapping(value = "options")
    @ApiOperation(value = "项目下拉选项")
    @ApiOperationSupport(order = 1)
    public ActionResult<List<Option<String>>> options(TblDiagItem entity) {
        return ActionResult.success(diagItemService.listOptions(entity));
    }

    @GetMapping(value = "checkCode")
    @ApiOperation(value = "校验编码是否唯一", notes = "true:唯一, false:已存在")
    @ApiOperationSupport(order = 2)
    public ActionResult<Boolean> checkCode(TblDiagItem category) {
        return ActionResult.success(diagItemService.checkCodeUnique(category));
    }

    @GetMapping(value = "{cId}/get")
    @ApiOperation(value = "表单数据")
    @ApiOperationSupport(order = 3)
    //@RequiresPermissions(value = {"system:diagItem:get", "system:diagItem:update"}, logical = Logical.OR)
    public ActionResult<TblDiagItem> get(@PathVariable String cId) {
        return ActionResult.success(diagItemService.getById(cId));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增")
    @PreventDuplicateSubmit
    @PostMapping(value = "add")
    @Log(title = "医嘱项目", actionType = ActionType.INSERT)
    //@RequiresPermissions(value = {"system:diagItem:insert"}, logical = Logical.OR)
    public ActionResult<TblDiagItem> add(@Validated @RequestBody TblDiagItem category) {
        diagItemService.insert(category);
        return ActionResult.success(category);
    }

    @PutMapping(value = "update")
    @ApiOperation(value = "修改")
    @ApiOperationSupport(order = 5)
    @Log(title = "医嘱项目", actionType = ActionType.UPDATE)
    //@RequiresPermissions(value = {"system:diagItem:update"})
    public ActionResult<Void> update(@Validated @RequestBody TblDiagItem entity) {
        TblDiagItem target = diagItemService.getById(entity.getId());
        Validate.notNull(null == target, "医嘱项目不存在");
        Validate.isTrue(diagItemService.checkCodeUnique(entity), "分类名称已存在");
        return ActionResult.judge(diagItemService.updateById(entity));
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除", notes = "支持批量删除")
    @ApiOperationSupport(order = 6)
    @Log(title = "医嘱项目", actionType = ActionType.DELETE)
    //@RequiresPermissions(value = {"system:diagItem:delete"})
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "删除的编号不能为空") List<String> ids) {
        int count = diagItemService.removeByIds(ids);
        return ActionResult.success(String.format("操作成功，共删除%d条记录", count));
    }
}
